package thread;

/**
 * Description:
 * Date: 2024-03-06
 */
public class Demo5 {   //卡锁
    private static Object joker1 = new Object();
    private static Object joker2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (joker1) {
                try {
                    Thread.sleep(1000);//睡眠才能让让joker1 和2 都先拿到一把锁
                                              //再分别阻塞等对方解锁 就卡住了
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (joker2) {
                    System.out.println("t1 加锁成功");
                }
            }
        });


        Thread t2 = new Thread(() -> {
            synchronized (joker2) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (joker1) {
                    System.out.println("t2 加锁成功");
                }
            }
        });
        t1.start();
        t2.start();
    }

}
